package oakclub.global;

import org.picocontainer.DefaultPicoContainer;
import org.picocontainer.behaviors.Caching;

public class Container
{
    private static DefaultPicoContainer container;

    static
    {
        container = new DefaultPicoContainer(new Caching());
    }

    public static void addComponent(Enums.Component key, Object instance)
    {
        container.addComponent(key, instance);
    }

    public static Object getComponent(Enums.Component key)
    {
        return container.getComponent(key);
    }

    public static Object getComponent(Class clazz)
    {
        Object component = container.getComponent(clazz);
        if(component != null)
        {
            return component;
        }

        String packageName = clazz.getPackage().getName();
        if(packageName.equals("oakclub.service") || packageName.equals("oakclub.model"))
        {
            container.addComponent(clazz);
            return container.getComponent(clazz);
        }

        return null;
    }
}
